#! /usr/bin/env python
# $Id: intro.py,v 1.4 2004/11/27 09:57:33 krawczyk Exp $

import os, sys
import pygame
import config, scroller, flocke, mixer
from pygame.locals import *

def Init():
    """Init function."""
    global background, objects, scr, scroller, sound, titelpic, titelpicwidth

    SCROLL = config.introscroller
    objects = pygame.sprite.RenderUpdates()
    config.mixer.load(SCROLL["music"])
    config.mixer.play(-1) #Endlosschleife
    scr = pygame.display.get_surface()
    background = pygame.image.load(SCROLL["backgroundfile"])
    background = background.convert(scr)
    titelpic   = pygame.image.load(SCROLL["titelpicfile"]).convert_alpha()
    titelpicwidth = titelpic.get_width()
    titelpic.set_alpha(255, RLEACCEL)
    scr.blit(background, (0,0))
    width = config.scrsize[0]
    scroller = scroller.Smoothscroll(SCROLL["text"], (0, SCROLL["pos"]), width, SCROLL["speed"])
    flocke.Flocke(objects)
    #print objects

def DeInit():
    global background, objects, scr, scroller, sound, titelpic, titelpicwidth

    del objects
    del titelpicwidth
    del titelpic
    del background
    del scroller

def main():
    "Simple Smooth scroller"

    global scroller, objects
    Init()
    clock = pygame.time.Clock() #Zeitobjekt
    fps = config.introscroller['fps']
    ende = -2
    flocken = 1
    pygame.display.update()
    while ende:
        to_update = []
        #Clean
        objects.clear(scr, background)
        #to_update.extend(scroller.Erase(scr, background))
        scr.blit(background, background.get_rect(), background.get_rect())
        #Update
        objects.update()
        scroller.Update()
        #Paint
        to_update.extend(scroller.Draw(scr))
        to_update.extend(objects.draw(scr))
        to_update.append(scr.blit(titelpic, ((config.scrsize[0]-titelpicwidth)/2, 50)))
        pygame.display.update(to_update)
        #pygame.display.flip()
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == QUIT or event.type == MOUSEBUTTONDOWN or (event.type == KEYDOWN and event.key in [K_ESCAPE,K_SPACE]):
                mixer.mixer.fadeout(1333)
                ende = 25
        if ende > 0:
            ende -= 1
        else:
            if ende != -1:
                ende -= 1
                if ende % 122 == 0:
                    if flocken <= config.introscroller["flocken"]:
                        flocken += 1
                        flocke.Flocke(objects)
                    else:
                        ende = -1
    print "flocken=",flocken
    #
    if "interactive" in config.__dict__:
        if config.interactive:
            import code, readline
            code.interact("Leise rieselt der Schnee; interactive Konsole:", raw_input, globals())
    #
    pygame.time.wait(750)
    DeInit()
